package com.eqdd.yiqidian.ui.gongneng.renshi.gonggao;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.CreateNoticeActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditRadioBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.test.ExpandBean;
import com.eqdd.yiqidian.test.SelectExpandActivity;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.Calendar;
import java.util.Date;

public class PublishNoticeActivity extends SlimBaseMultiEditActivity {
    private CreateNoticeActivityCustom dataBinding;
    private String msg;
    private ExpandBean expandBean;
    private int type;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_notice);
        type = getIntent().getIntExtra(Config.TYPE, 0);
        String title = "发布公告";
        if (type == 1) {
            title = "发布通知";
        }

        initTopTitleBar(View.VISIBLE, title);
        initTopRightText("发布", v -> {
            if (checkParam()) {
                boolean left = ((SlimEditRadioBean) datas.get(1)).isLeft();
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_NOTICE)
                        .params("companyId", user.getCompanyId())
                        .params("userGuid", user.getGuid())
                        .params("noticeName", datas.get(0).getContent())
                        .params("objectType", left ? 0 : 1)
                        .params("objectDepartId", left ? 0 : expandBean.getId())
                        .params("noticeTheme", datas.get(left ? 2 : 3).getContent())
                        .params("noticeContent", datas.get(left ? 3 : 4).getContent())
                        .params("duty", "")
                        .params("noticeCycle", datas.get(left ? 4 : 5).getContent())
                        .params("isAdmin", user.getIsAdmin() > 0 ? 1 : 0)
                        .params("createDepartId", user.getIsAdmin() > 0 ? 0 : user.getDepartId())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });

    }


    private boolean checkParam() {

        boolean left = ((SlimEditRadioBean) datas.get(1)).isLeft();
        if (TextUtils.isEmpty(datas.get(0).getContent())) {
            msg = "请选择公告类型";
            return false;
        }
        if (!left && TextUtils.isEmpty(datas.get(2).getContent())) {
            msg = "请选择发布对象";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 2 : 3).getContent())) {
            msg = "请输入公告主题";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 3 : 4).getContent())) {
            msg = "请选择公告内容";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 4 : 5).getContent())) {
            msg = "请选择处理时限";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 5 : 6).getContent())) {
            msg = "请选择定时时间";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                initPublishType();
                break;
            case 2:
                startActivityForResult(SelectExpandActivity.class, null, Config.SELECT_EXPAND);
                break;
            case 5:
                initLimit(p);
                break;
            case 6:
                initTime(p);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditEnterBean("公告类型", "", 0, true, "请选择"));
        datas.add(new SlimEditRadioBean("全体员工", "部门", true, 1));
        datas.add(new SlimEditEnterBean("公文主题", "", 3, "请输入"));
        datas.add(new SlimEditEnterBean("公文内容", "", 4, "请输入"));
        datas.add(new SlimEditEnterBean("处理时限", "", 5, true, "请选择"));
        datas.add(new SlimEditEnterBean("定时", "", 6, true, "请选择"));
        slimAdapterEx.register(R.layout.list_item_38, new SlimInjector<SlimEditRadioBean>() {
            @Override
            public void onInject(SlimEditRadioBean data, IViewInjector injector) {
                if (type == 1) {
                    data.setLeft(false);
                    injector.checked(R.id.rb_reject, true)
                            .text(R.id.rb_reject, data.getRightContent())
                            .gone(R.id.rb_agree);
                    if (slimAdapterEx.getDataItem(2).getPosition() != 2) {
                        slimAdapterEx.getData().add(2, new SlimEditEnterBean("发布对象", "", 2, true, "请选择部门"));
                        slimAdapterEx.notifyItemInserted(2);
                    }
                } else {
                    injector.text(R.id.rb_reject, data.getLeftContent())
                            .text(R.id.rb_agree, data.getRightContent())
                            .checked(data.isLeft() ? R.id.rb_reject : R.id.rb_agree, true)
                            .checked(data.isLeft() ? R.id.rb_agree : R.id.rb_reject, false)
                            .with(R.id.rb_reject, v -> {
                                ((RadioButton) v).setOnCheckedChangeListener((buttonView, isChecked) -> {
                                    if (isChecked) {
                                        data.setLeft(true);
                                        injector.checked(R.id.rb_agree, !isChecked);
                                        if (slimAdapterEx.getDataItem(2).getPosition() == 2) {
                                            expandBean = null;
                                            slimAdapterEx.getData().remove(2);
                                            slimAdapterEx.notifyItemRemoved(2);
                                        }
                                    }
                                });
                            })
                            .with(R.id.rb_agree, v -> {
                                ((RadioButton) v).setOnCheckedChangeListener((buttonView, isChecked) -> {
                                    if (isChecked) {
                                        data.setLeft(false);
                                        injector.checked(R.id.rb_reject, !isChecked);
                                        if (slimAdapterEx.getDataItem(2).getPosition() != 2) {
                                            slimAdapterEx.getData().add(2, new SlimEditEnterBean("发布对象", "", 2, true, "请选择部门"));
                                            slimAdapterEx.notifyItemInserted(2);
                                        }
                                    }
                                });
                            });
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_EXPAND) {
            expandBean = data.getParcelableExtra("bean");
            slimAdapterEx.getDataItem(2).setContent(expandBean.getLabel());
            slimAdapterEx.notifyItemChanged(2);
        }
    }

    public void initPublishType() {
        String[] array = new String[]{"公告", "通知", "通告"};
        if (AppConstent.user.getIsAdmin() > 0) {
            array = new String[]{"公告", "通告"};
        } else if (type == 1) {
            array = new String[]{"通知"};
            slimAdapterEx.getDataItem(0).setContent("通知");
            slimAdapterEx.notifyItemChanged(0);
        }
        String[] finalArray = array;
        ChooseAlertView.getBottomChoose(this, null, array, (o, pos) -> {
            slimAdapterEx.getDataItem(0).setContent(finalArray[pos - 1]);
            slimAdapterEx.notifyItemChanged(0);

        }).show();
    }

    public void initLimit(int p) {
        String[] array = new String[]{"1小时内", "2小时内", "4小时内", "8小时内", "24小时内", "两个工作日", "三个工作日内"};
        ChooseAlertView.getBottomChoose(this, null, array, (o, pos) -> {
            slimAdapterEx.getDataItem(p).setContent(array[pos - 1]);
            slimAdapterEx.notifyItemChanged(p);

        }).show();
    }

    Calendar now = Calendar.getInstance();

    public void initTime(int p) {
        now.setTime(new Date());
        TimePickerView time = new TimePickerView.Builder(this, (date, v) -> {
            String startDate = DateUtil.parse(date, "yyyy-MM-dd HH:mm");
            slimAdapterEx.getDataItem(p).setContent(startDate);
            slimAdapterEx.notifyItemChanged(p);
            now.setTime(date);
        })
                .setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, true, true, false})
                .setRange(2017, 2050)
                .setDate(now)
                .build();
        time.show();
    }
}
